﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace ProjetDll
{
    public static class LastFmApi
    {

        #region Champs

        private static string url_RechercheConcertsArtiste = "http://ws.audioscrobbler.com/2.0/?method=artist.getevents&artist=#artistName#&api_key=4f761544db2fa7d3d12eccd7ad56e1c4";

        private static string url_RechercheConcertsSalle = "http://ws.audioscrobbler.com/2.0/?method=geo.getevents&location=#villeName#&api_key=4f761544db2fa7d3d12eccd7ad56e1c4";

        private static string url_getAllArtistsByName = "http://ws.audioscrobbler.com/2.0/?method=artist.search&artist=#artistName#&api_key=4f761544db2fa7d3d12eccd7ad56e1c4";

        #endregion

        #region Méthodes

        public static List<Event> getAllEventByArtist(string nomArtist)
        {
            List<Event> lesEvents = new List<Event>();

            string url_RechercheConcertsArtist = url_RechercheConcertsArtiste.Replace("#artistName#", nomArtist);

            IEnumerable<XElement> xLesEvents = XDocument.Load(url_RechercheConcertsArtist).Root.Element("events").Elements("event");
            
            int i = 1;

            foreach (XElement xUnEvent in xLesEvents)
            {
                XElement xUneVenue = xUnEvent.Element("venue");
                XElement xUneLocalisation = xUneVenue.Element("location");
                Localisation uneLocalisation = new Localisation(xUneLocalisation.Element("street").Value,
                                                                xUneLocalisation.Element("city").Value,
                                                                xUneLocalisation.Element("country").Value,
                                                                xUneLocalisation.Element("postalcode").Value);

                Venue uneVenue = new Venue(xUneVenue.Element("name").Value, xUneVenue.Element("url").Value, uneLocalisation);

                Artist unArtist = new Artist(nomArtist);
            
                Event unEvent = new Event(xUnEvent.Element("id").Value,
                                       xUnEvent.Element("title").Value,
                                       xUnEvent.Element("description").Value,
                                       xUnEvent.Element("startDate").Value,
                                       xUnEvent.Element("image").Value,
                                       uneVenue,
                                       unArtist);

                lesEvents.Add(unEvent);
                i++;
            }

            return lesEvents;
        }

        public static List<Event> getGeoEvents(string strLocalisation)
        {
            List<Event> lesEvents = new List<Event>();

            string url_RechercheConcertsParSalle = url_RechercheConcertsSalle.Replace("#villeName#", strLocalisation);

            IEnumerable<XElement> xLesEvents = XDocument.Load(url_RechercheConcertsParSalle).Root.Element("events").Elements("event");

            int i = 1;

            foreach (XElement xUnEvent in xLesEvents)
            {
                XElement xUneVenue = xUnEvent.Element("venue");
                XElement xUneLocalisation = xUneVenue.Element("location");
                Localisation uneLocalisation = new Localisation(xUneLocalisation.Element("street").Value,
                                                                xUneLocalisation.Element("city").Value,
                                                                xUneLocalisation.Element("country").Value,
                                                                xUneLocalisation.Element("postalcode").Value);

                Venue uneVenue = new Venue(xUneVenue.Element("name").Value, xUneVenue.Element("url").Value, uneLocalisation);

                Event theEvent = new Event(xUnEvent.Element("id").Value,
                                       xUnEvent.Element("title").Value,
                                       xUnEvent.Element("description").Value,
                                       xUnEvent.Element("startDate").Value,
                                       xUnEvent.Element("image").Value,
                                       uneVenue);

                lesEvents.Add(theEvent);
                i++;

            }
            return lesEvents;
        }

        public static List<Artist> getAllArtistByName(string nameArtist)
        {
            List<Artist> lesArtists = new List<Artist>();

            string urlArtistByName = url_getAllArtistsByName.Replace("#artistName#", nameArtist);

            int i = 1;

            IEnumerable<XElement> xLesArtists = XDocument.Load(urlArtistByName).Root.Element("results").Element("artistmatches").Elements("artist");

            foreach (XElement xUnArtist in xLesArtists)
            {
                Artist unArtist = new Artist(xUnArtist.Element("mbid").Value,
                                             xUnArtist.Element("name").Value,
                                             xUnArtist.Element("url").Value,
                                             xUnArtist.Element("image").Value);
                lesArtists.Add(unArtist);
                i++;
            }
            
            return lesArtists;
        }

        #endregion

    }
}
